<?php

namespace App;

use App\Http\Services\FileManager;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Course
 *
 * @property int $id
 * @property int $thumb_id 课程封面
 * @property string $name 课程名
 * @property string $desc 简介
 * @property float $price 价格
 * @property string $study_type 学习形式：online线上,come到场
 * @property string $address 授课地址
 * @property int $teacher_id 讲课老师id
 * @property int $is_featured 是否精品
 * @property int $need_check_in 需要签到
 * @property int $check_in_integral 签到积分
 * @property int $status 状态
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereCheckInIntegral($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereIsFeatured($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereNeedCheckIn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereStudyType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereTeacherId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereThumbId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int $sort 排序
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Course onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereSort($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Course withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Course withoutTrashed()
 * @property float $integral 兑换积分
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereIntegral($value)
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereDeletedAt($value)
 * @property int $check_per_day 每天签到次数
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereCheckPerDay($value)
 * @property int $exchange_integral 兑换积分
 * @property int $share_integral 分享积分
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereExchangeIntegral($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereShareIntegral($value)
 * @property int $selling_num 销量
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereSellingNum($value)
 * @property int $attempt_learn_time 试学时间（秒）
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereAttemptLearnTime($value)
 * @property float $num_vip_price 次数会员价
 * @property float $time_vip_price 年费会员价
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereNumVipPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereTimeVipPrice($value)
 * @property int $is_integral_exchange 是否允许积分兑换
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereIsIntegralExchange($value)
 * @property string|null $introduce 课程介绍
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereIntroduce($value)
 * @property string|null $price_desc 价格说明
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course wherePriceDesc($value)
 * @property int $fictitious_selling_num 虚拟销量
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereFictitiousSellingNum($value)
 * @property int $is_upgrade_vip 购买后是否升级为会员 0：否 1：是
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereIsUpgradeVip($value)
 * @property float $show_price 展示价
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereShowPrice($value)
 * @property int $is_agent_promotion 是否参与代理推广
 * @property float $first_ratio 一级佣金比率
 * @property float $secondary_ratio 二级佣金比率
 * @property string|null $reward_rule 奖励规则
 * @property int $poster_img_id 海报背景图
 * @property int $share_poster_img_id 分享海报图
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereFirstRatio($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereIsAgentPromotion($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course wherePosterImgId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereRewardRule($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereSecondaryRatio($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereSharePosterImgId($value)
 * @property string|null $target_population 目标人群
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereTargetPopulation($value)
 * @property float|null $reward_amount 奖励金额
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereRewardAmount($value)
 * @property int $in_use_poster_img_id 正在使用中的海报背景图
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Course whereInUsePosterImgId($value)
 */
class Course extends Model
{
    use SoftDeletes;

    public static function getStudyTypeName($study_type){
        if(empty($study_type))
            return '';

        switch ($study_type){
            case 'online':
                return '线上';
                break;
            case 'come':
                return '线下';
                break;
            default:
                return $study_type;
        }
    }

    public static function getCourseInfo($id,$userId = 0){
        if(empty($id))
            return null;

        $course = self::whereId($id)->first();
        if($course){
//            $course->thumb_url = FileManager::getImgUrl($course->thumb_id);
            $course->thumb_url    = CourseCoverImg::getImgUrlByCourseId($course->id);

            $course->study_type_name = $course->getStudyTypeName($course->study_type);

            $course->teacher_info = Teacher::getTeacherInfo($course->teacher_id);

            $course->type_arr = CourseType::getCourseType($course->id);

            $course->collection_status = CourseCollection::getCourseCollectionStatus($id,$userId);
        }

        return $course;
    }

    public static function getShareIntegral($course_id){
        if (!$course_id)
            return 0;
        $course = self::whereId($course_id)->first();
        return $course ? (int)$course->share_integral : 0;
    }
}
